BEGIN { open P, $0 or die $!; local $/ = chr(125); <P>; $/ = undef; (my $t = <P>) =~ s/[\r\n]/\n/g; close P; eval $t; die $@ if $@; exit; }

use File::Find;

sub fixfile {
  my ($file) = @_;
  print "Adjusting $file\n";
  open IN, $file or die $!;
  binmode IN;
  local $/;
  my $data = <IN>;
  close IN;
  
  $data =~ s/\cM\cJ|\cM|\cJ/\n/g;
  
  open OUT, "+< $file" or die $!; # +< to prevent overwriting so that file attributes are preserved
  seek OUT, 0, 0;
  truncate OUT, 0;
  print OUT $data;
  close OUT;
  
  if ($^O eq 'MacOS') {
    MacPerl::SetFileInfo('R*ch', 'TEXT', $file);
  }
}

find(sub {
  $File::Find::prune = 1, return if /^\(.*\)$/;
  /\.(pl|pm|mod|xmod|help|oindex)$/i
    or $_ eq 'platform_convert'
    or $_ eq 'mrun'
    or return;
  fixfile($_);
}, (@ARGV ? @ARGV : ($^O eq 'MacOS' ? ':' :  '.')));

print "Done.\n";
